﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace LDLib
{
    /// <summary>
    /// Resources class. Keeps track of the loaded resources, such
    /// as meshes, textures and shaders.
    /// </summary>
    public static class R
    {
        public static Dictionary<string, Mesh> Meshes { get; private set; }
        public static Dictionary<string, int> Textures { get; private set; }
        public static Dictionary<string, Shader> Shaders { get; private set; }
        public static Dictionary<string, Material> Materials { get; private set; }

        public static Dictionary<string, BitmapFont> Fonts { get; private set; }

        /// <summary>
        /// Loads the 'core' resources, such as default shaders
        /// </summary>
        public static void LoadCoreResources()
        {
            Meshes = new Dictionary<string, Mesh>();
            Textures = new Dictionary<string, int>();
            Shaders = new Dictionary<string, Shader>();
            Materials = new Dictionary<string, Material>();
            Fonts = new Dictionary<string, BitmapFont>();

            // Default colored shader
            Shader coloredShader = AssetLoader.LoadShaderInternal("Colored Shader", "DefaultVertexShader.glsl", "DefaultFragmentShader.glsl");
            coloredShader.AddAttribLocations("in_position", "in_normal", "in_texcoord");
            coloredShader.AddFragDataLocations("out_color");
            coloredShader.AddUniforms("world", "view", "projection", "color");

            // GUI shader
            Shader guiShader = AssetLoader.LoadShaderInternal("GUI", "GUIVertexShader.glsl", "GUIFragmentShader.glsl");
            guiShader.AddAttribLocations("in_position", "in_texcoord");
            guiShader.AddFragDataLocations("out_color");
            guiShader.AddUniforms("world", "diffuseColor", "colorMap");

            // Default textured shader
            /*
            vertexShaderSource = LoadShaderResource(ass.GetManifestResourceStream("LDLib.Assets.DefaultTexturedVertexShader.glsl"));
            fragmentShaderSource = LoadShaderResource(ass.GetManifestResourceStream("LDLib.Assets.DefaultTexturedFragmentShader.glsl"));
            Shader s2 = new Shader(vertexShaderSource, fragmentShaderSource);
            s2.AddAttribLocations("in_position", "in_normal", "in_texcoord");
            s2.AddFragDataLocations("out_color");
            s2.AddUniforms("world", "view", "projection", "colorMap");
            Shaders.Add("Textured Shader", s2);
            */
            // Default colored material


            // Default textured material
        }

        /// <summary>
        /// Loads a shader source from the embedded resources
        /// </summary>
        /// <param name="s">Stream to load</param>
        /// <returns>A string representation of the shader source</returns>
        private static string LoadShaderResource(Stream s)
        {
            if (s == null) return "";

            string result = "";
            StreamReader reader = new StreamReader(s);
            while(reader.Peek() != -1)
            {
                result += reader.ReadLine() + "\n";
            }
            return result;
        }
    }
}
